package com.yuyue.springFramework.note07.entity;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * 定义Condition 的实现类，重写匹配方法，从工厂中查找并判断IService的实现类是否存在
 */
public class MyCondition2 implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取bean工厂
        ConfigurableListableBeanFactory configurableListableBeanFactory = context.getBeanFactory();
        //获取IService的bean
        Map<String, IService> map = configurableListableBeanFactory.getBeansOfType(IService.class);
        //以集合是否为空作为判断依据
        return map.isEmpty();
    }
}
